package com.components.update.http

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import com.components.update.FilePoint
import com.components.update.callback.DownloadListener
import java.io.File

class DownloadManager {
    //默认下载目录
    private var DEFAULT_FILE_DIR: String? = null

    //文件下载任务索引，String为url,用来唯一区别并操作下载的文件
    private val mDownloadTasks: MutableMap<String, DownloadTask?>

    private lateinit var context: Context

    /**
     * 下载文件
     */
    fun download(vararg urls: String?,repeatDownLoad: Boolean = false) {
        //单任务开启下载或多任务开启下载
        var i = 0
        val length = urls.size
        while (i < length) {
            val url = urls[i]
            if (mDownloadTasks.containsKey(url)) {
                mDownloadTasks[url]!!.start(repeatDownLoad)
            }
            i++
        }
    }

    fun download(url: String, repeatDownLoad: Boolean = false) {
        //单任务开启下载或多任务开启下载
        if (mDownloadTasks.containsKey(url)) {
            mDownloadTasks[url]!!.start(repeatDownLoad)
        }
    }

    // 获取下载文件的名称
    private fun getFileName(url: String): String {
        return url.substring(url.lastIndexOf("/") + 1)
    }

    /**
     * 暂停
     */
    fun pause(vararg urls: String?) {
        //单任务暂停或多任务暂停下载
        var i = 0
        val length = urls.size
        while (i < length) {
            val url = urls[i]
            if (mDownloadTasks.containsKey(url)) {
                mDownloadTasks[url]!!.pause()
            }
            i++
        }
    }

    /**
     * 取消下载
     */
    fun cancel(vararg urls: String?) {
        //单任务取消或多任务取消下载
        var i = 0
        val length = urls.size
        while (i < length) {
            val url = urls[i]
            if (mDownloadTasks.containsKey(url)) {
                mDownloadTasks[url]!!.cancel()
            }
            i++
        }
    }

    /**
     * 添加下载任务
     */
    fun add(context: Context, url: String, l: DownloadListener?) {
        this.context = context
        add(context, url, null, null, l)
    }

    /**
     * 添加下载任务
     */
    fun add(context: Context, url: String, filePath: String?, l: DownloadListener?) {
        this.context = context
        add(context, url, filePath, null, l)
    }

    /**
     * 添加下载任务
     */
    private fun add(
        context: Context,
        url: String,
        filePath: String?,
        fileName: String?,
        l: DownloadListener?
    ) {
        this.context = context
        var filePath = filePath
        var fileName = fileName
        if (TextUtils.isEmpty(filePath)) { //没有指定下载目录,使用默认目录
            filePath = defaultDirectory
        }
        if (TextUtils.isEmpty(fileName)) {
            fileName = getFileName(url)
        }
        mDownloadTasks[url] = DownloadTask(context, FilePoint(url, filePath, fileName), l)
    }

    /**
     * 默认下载目录
     * @return
     */
    private val defaultDirectory: String?
        get() {
            if (TextUtils.isEmpty(DEFAULT_FILE_DIR)) {
                DEFAULT_FILE_DIR = (context.externalCacheDir!!.absolutePath
                        + File.separator)

            }
            return DEFAULT_FILE_DIR
        }

    /**
     * 取消下载
     */
    fun isDownloading(vararg urls: String?): Boolean {
        //这里传一个url就是判断一个下载任务
        //多个url数组适合下载管理器判断是否作操作全部下载或全部取消下载
        var result = false
        var i = 0
        val length = urls.size
        while (i < length) {
            val url = urls[i]
            if (mDownloadTasks.containsKey(url)) {
                result = mDownloadTasks[url]!!.isDownloading
            }
            i++
        }
        return result
    }

    companion object {
        @SuppressLint("StaticFieldLeak")
        private var INSTANCE: DownloadManager? = null
            get() {
                if (field == null) {
                    field = DownloadManager()
                }
                return field
            }

        @Synchronized
        fun get(): DownloadManager {
            return INSTANCE!!
        }

    }

    init {
        mDownloadTasks = HashMap()
    }
}